package com.maduar.mybatis.demo.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maduar.mybatis.demo.vo.PageInterface;

import java.util.Objects;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-04-10 18:34
 */
public class DemoUtils {

    private DemoUtils() {
    }

    public static final int CURRENT = 1;
    public static final int PAGE_SIZE = 10;

    public static <T> String toString(T t) {
        if (Objects.isNull(t)) {
            return "";
        }

        return JSON.toJSONString(t);
    }

    public static <T> Page<T> getPage(PageInterface pageInterface) {
        if (Objects.isNull(pageInterface)) {
            return getPage();
        }

        int p = CURRENT;
        int s = PAGE_SIZE;

        Integer page = pageInterface.getPage();
        if (Objects.nonNull(page)) {
            p = page;
        }

        Integer size = pageInterface.getSize();
        if (Objects.nonNull(size)) {
            s = size;
        }
        return new Page<>(p, s);
    }

    public static <T> Page<T> getPage() {
        return new Page<>(CURRENT, PAGE_SIZE);
    }
}
